//
//  GYTaskDownloadAlertView.m
//  LockManagement
//
//  Created by 丁燕春 on 2018/7/28.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYTaskDownloadAlertView.h"

@interface GYTaskDownloadAlertView()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>;
@property (weak, nonatomic) IBOutlet UITableView *messageTableView;
@property (weak, nonatomic) IBOutlet UILabel *offlineTimeLable;
@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

@property (weak, nonatomic) IBOutlet UIView *downloadView;
@property (weak, nonatomic) IBOutlet UITextField *offineTF;


/** 标题 */
@property (nonatomic, strong) NSArray *titles;

/** 内容 */
@property (nonatomic, strong) NSArray *contens;


@end
@implementation GYTaskDownloadAlertView

+ (instancetype)initWithFrame:(CGRect)frame {
    GYTaskDownloadAlertView *downloadView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
    downloadView.frame = frame;
    
    return downloadView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.messageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
    
    self.downloadBtn.layer.cornerRadius = 4;
    self.cancelBtn.layer.cornerRadius = 4;
    
    self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.4];
    self.downloadView.layer.cornerRadius = 4;
    self.downloadView.layer.masksToBounds = YES;
}

- (void)setTask:(GYTask *)task {
    _task = task;
    
    task.offineTime = @"0";
}


- (IBAction)onClick:(UIButton *)sender {
    if (sender.tag == 1) {
        NSLog(@"下载任务列表....===下载完成之后要溢出这个view");//这里要下载 这个任务数据
        BOOL result = [GYTask createTaskTable];
        if (result) {
            //把该任务储存在数据库中
            [GYTask insertTask:self.task];
            
            //移除view 应该要等下载完后才能溢出view  这里应该显示下载进度
            [self removeFromSuperview];
            
//            [SVProgressHUD showSuccessWithStatus:@"任务下载成功!"]
//            ;
            NSLog(@"offine-------------%@",self.task.offineTime);
            //这里要发送蓝牙授权命令
            self.sendAutoCommandCallBack(self.task);
        }
    }else {
        [self removeFromSuperview];
    }
}

/**
 显示下载进度视图
 */
- (void)showDownloadProgressView {
    //1.首先要移除下载
    [self.downloadView removeFromSuperview];
    
    //添加下载进度View
    UIView *progressView = [[UIView alloc] initWithFrame:CGRectMake(30, self.height-60, self.width-60, 120)];
    progressView.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, progressView.width-20, 30)];
    titleLable.text = @"任务下发";
    titleLable.textColor = BackGroundColor;
    titleLable.font = [UIFont systemFontOfSize:20];
    
    [progressView addSubview:titleLable];
    
    UILabel *markLbale = [[UILabel alloc] initWithFrame:CGRectMake(0, 59, progressView.width, 1)];
    markLbale.backgroundColor = BackGroundColor;
    [progressView addSubview:markLbale];
    
    UIProgressView *progress = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 80, progressView.width-20, 2)];
    progress.progress = 0.33;
    [progressView addSubview:progress];
    
    UILabel *progressLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 85, 50, 30)];
    progressLable.textColor = [UIColor blackColor];
    progressLable.text = @"33%";
    [progressView addSubview:progressLable];
    
    UILabel *sumLable = [[UILabel alloc] initWithFrame:CGRectMake(progressView.width-60, 85, 50, 30)];
    sumLable.textColor = [UIColor blackColor];
    sumLable.text = @"1/3";
    [progressView addSubview:sumLable];
    
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.offineTF.isFirstResponder) {
        [self.offineTF resignFirstResponder];
        
        return;
    }
    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    CGPoint currentPoin = [touch locationInView:self]; //返回触摸点在视图中的当前坐标
    if (!CGRectContainsPoint(self.downloadView.frame, currentPoin)) {
        [self removeFromSuperview];
    }
}

#pragma mark -- 放弃第一响应者

- (IBAction)registerFirstRespond:(UITextField *)sender {
    [sender resignFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text intValue] > 60) {
        textField.text = @"60";
    }
    self.task.offineTime = textField.text;
}



#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }
    cell.textLabel.text = self.titles[indexPath.row];
    cell.detailTextLabel.text = self.contens[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 33;
}

#pragma mark -- 懒加载

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@"任务类型",@"锁具名称	",@"区域名称",@"授权人员",@"开始时间",@"结束时间"];
    }
    return _titles;
}

- (NSArray *)contens {
    if (!_contens) {
        NSData *areaData = [self.task.areas dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *areas = [NSJSONSerialization JSONObjectWithData:areaData options:NSJSONReadingMutableLeaves error:nil];
        NSString *lockString = @"";
        NSString *areaString = @"";
        for (NSDictionary *dic in areas) {
            NSString *lockID = [dic objectForKey:@"lockID"];
            NSString *areaName = [dic objectForKey:@"areaName"];
            lockString = [lockString stringByAppendingString:lockID];
            areaString = [areaString stringByAppendingString:areaName];
            
            if (areas.count > 1) {
                lockString = [lockString stringByAppendingString:@","];
                areaString = [areaString stringByAppendingString:@","];
            }
        }
        _contens = @[self.task.taskType == 0 ? @"巡逻" : (self.task.taskType == 2 ? @"开锁" : @"关锁"),
                     lockString,
                     areaString,
                     self.task.authName,
                     self.task.taskBegin,
                     self.task.taskEnd];
    }
    
    return _contens;
}


- (void)dealloc {
    NSLog(@"GYTaskDownloadAlertView===============dealloc");
}

@end
